package com.example.client;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import util.PermissionUtil;

public class PermissionHungry extends AppCompatActivity {
    private static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_SMS,
            Manifest.permission.SEND_SMS,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS
    };
    private static final int REQUEST_CODE_ALL = 1;
    private static final int REQUEST_CODE_CONTACTS = 2;
    private static final int REQUEST_CODE_SMS = 3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_permission_hungry);

        PermissionUtil.checkPermission(this,PERMISSIONS,REQUEST_CODE_ALL);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUEST_CODE_ALL:
                if(PermissionUtil.checkGrant(grantResults)){
                    Log.e("ning", "通讯录和短信权限获取成功: " );
                }else{
                    for (int i = 0;i < permissions.length; i++){
                        if (grantResults[i]!= PackageManager.PERMISSION_GRANTED){
                            switch (permissions[i]){
                                case  Manifest.permission.READ_SMS:
                                case Manifest.permission.SEND_SMS:
                                    Toast.makeText(this,"短信权限申请失败",Toast.LENGTH_LONG).show();
                                    break;
                                case Manifest.permission.READ_CONTACTS:
                                case Manifest.permission.WRITE_CONTACTS:
                                    Toast.makeText(this,"通讯录权限读取失败",Toast.LENGTH_LONG).show();
                                    break;
                            }
                        }
                    }
                }
                break;
        }

    }
}